package org.oos.model;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.WeakHashMap;

import org.oos.atom.elements.Link;
import org.oos.util.Log;

/**
 * Wrap of a {@link Link} that represents the profile picture of a user or
 * author.
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public class UserAvatar implements Serializable {
	private static final long serialVersionUID = -8309345110435644643L;

	// private Link link;

	private String url;

	// private static final String REL = "avatar";
	private static final String DEFAULT_AVATAR_PATH = "http://11870.com/multimedia/avatar/xg_avatar.png";
	/**
	 * Default Avatar, the location of this avatar is: <br />
	 * <img src="http://11870.com/multimedia/avatar/xg_avatar.png"> <br />
	 * <a href="http://11870.com/multimedia/avatar/xg_avatar.png"
	 * >http://11870.com/multimedia/avatar/xg_avatar.png</a>
	 * 
	 */
	public static final UserAvatar DEFAULT_AVATAR = new UserAvatar();

	/**
	 * This enumeration describes the Sized that an avatar may come in:
	 * 
	 * <table border="1">
	 * <tr>
	 * <td>Small</td>
	 * <td>Medium</td>
	 * <td>Large</td>
	 * <td>Extra Large</td>
	 * </tr>
	 * <tr>
	 * <td>19x19</td>
	 * <td>30x30</td>
	 * <td>48x48</td>
	 * <td>70x70</td>
	 * </tr>
	 * <tr>
	 * <td><img src="http://11870.com/multimedia/avatar/pq_avatar.png"></td>
	 * <td><img src="http://11870.com/multimedia/avatar/md_avatar.png"></td>
	 * <td><img src="http://11870.com/multimedia/avatar/gd_avatar.png"></td>
	 * <td><img src="http://11870.com/multimedia/avatar/xg_avatar.png"></td>
	 * </tr>
	 * </table>
	 * 
	 * @author pablo.gonzalez@11870.com
	 * 
	 */
	public enum AvatarSize {
		SMALL("pq"), MEDIUM("md"), LARGE("gd"), XLARGE("xg");

		private String name;
		private Map<UserAvatar, String> pathCache = new WeakHashMap<UserAvatar, String>();

		private AvatarSize(String smallName) {
			this.name = smallName;
		}

		/**
		 * Provides with the path for the image for this {@link AvatarSize}
		 * 
		 * @param avatar
		 * @return
		 */
		CharSequence getPath(UserAvatar avatar) {
			if (!pathCache.containsKey(avatar)) {
				String resultUrl;
				if (avatar.url != null) {
					resultUrl = avatar.url;
				} else {
					// if there is no link add the default Avatar path
					resultUrl = DEFAULT_AVATAR_PATH;
				}
				// if it is the same size as original
				if (!resultUrl.contains(name)) {
					// check if the original is of other size and
					for (AvatarSize size : values()) {
						if (resultUrl.contains(size.name)) {
							resultUrl = resultUrl.replace(size.name, name);
						}
					}
				}
				pathCache.put(avatar, resultUrl);
			}
			return pathCache.get(avatar);
		}
	}

	private UserAvatar() {

	}

	public UserAvatar(String url) {
		super();
		this.url = url;
	}

	/**
	 * Provides with the {@link URL} for the image representing this
	 * {@link UserAvatar} at the provided {@link AvatarSize}
	 * 
	 * @param size
	 * @return
	 */
	public URL getImage(AvatarSize size) {
		try {
			return new URL(getImagePath(size));
		} catch (MalformedURLException e) {
			Log.e(e);
			return null;
		}
	}

	/**
	 * Provides with the path of the image representing this {@link UserAvatar}
	 * at the provided {@link AvatarSize}
	 * 
	 * @param size
	 * @return
	 */
	public String getImagePath(AvatarSize size) {
		return size.getPath(this).toString();
	}

}
